// pages/authDoctors/authDoctors.js
var app = getApp()

var data_cases = require("../../data/data_cases.js")
var util = require("../../utils/util.js")
var getAuth_url = 'https://www.hkzhaoscut.top/manage/getNeedVerifyDoctor'
var auth_url ="https://www.hkzhaoscut.top/manage/audit"
var pageNum = 2
var pageSize = 5


// 获取初始的病例数据
var getInitCases = function (that, token) {
  var managerToken = token
  // console.log(managerToken)
  wx.request({
    url: getAuth_url,
    header: {
      'Authorization': managerToken
    },
    data: {
      pageNum: 1,
      pageSize: pageSize
    },
    success: res => {
      // console.log('获取初始数据成功', res.data)
      if (res.data.code == 200) {
        console.log('获取初始数据成功', res.data)
        var list = res.data.data.list
        var tmplist = []
        if (list.length != 0) {
          list.forEach(function (item, index) {
            tmplist[index] = {}
            tmplist[index].doctorTokenId = item.doctorTokenId
            tmplist[index].doctorType = item.doctorType
            tmplist[index].identifyId = item.identifyId
            tmplist[index].password = item.password
            tmplist[index].province = item.province
            tmplist[index].status = item.status
            tmplist[index].username = item.username
            tmplist[index].wokerId = item.wokerId
          })
          var curCases = tmplist
          that.setData({
            doctorList: curCases
          })
        }

      }

    }
  })

}
// 获取更多的病例数据
var getMoreCases = function (that) {
  var managerToken = that.data.managerToken
  wx.request({
    url: getAuth_url,
    header: {
      'Authorization': managerToken
    },
    data: {
      pageNum: pageNum,
      pageSize: pageSize
    },
    success: res => {
      if (res.data.code == 200) {
        console.log('获取初始数据成功', res.data)
        var list = res.data.data.list
        var tmplist = []
        pageNum++
        if (list.length != 0) {
          list.forEach(function (item, index) {
            tmplist[index] = {}
            tmplist[index].doctorTokenId = item.doctorTokenId
            tmplist[index].doctorType = item.doctorType
            tmplist[index].identifyId = item.identifyId
            tmplist[index].password = item.password
            tmplist[index].province = item.province
            tmplist[index].status = item.status
            tmplist[index].username = item.username
            tmplist[index].wokerId = item.wokerId
          })
          var originCases = that.data.doctorList
          var curCases = originCases.concat(tmplist)
          that.setData({
            doctorList: curCases
          })
        }

      }

    }
  })

}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorList:[],//待审核的医生列表
    managerToken:'',//顾名思义
  },
  // 提交函数
  authSubmit(e){
    var target = e.currentTarget.dataset.pass
    var title = ""
    if (target==1){
      title="确定审核通过吗？"
    }else{
      title = "审核不通过！"
    }
    wx.showModal({
      title: '提交审核',
      content: title,
      success: function (res) {
        if (res.confirm) {
          console.log(e.currentTarget.dataset)
          var username = e.currentTarget.dataset.username
          wx.request({
            url: auth_url,
            header: {
              'Authorization': app.globalData.managerToken
            },
            data:{
              "username": username,
              "target": target
            },
            success(res){
              console.log(res)
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getInitCases(this,app.globalData.managerToken);
    this.setData({
      managerToken: app.globalData.managerToken
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //触底函数
      console.log("到底部了")
      getMoreCases(this)
    
  },

})